﻿using Admin2024.Domain.Entity.System;
using Admin2024.Domain.IDomainService.System.IPermissionService;
using Admin2024.Domain.Interface;
using Admin2024.Domain.ObjValue;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2024.Domain.DomainService.System.PermissionService
{
    public class PermissionManagementService : IPermissionManagementService
    {

        public readonly IRepository<AppPermission> PerRep;

        public PermissionManagementService(IRepository<AppPermission> perRep)
        {
            PerRep = perRep;
        }

        public async Task<DomainResult<AppPermission>> CreatePermission(AppPermission appPermission)
        {
            var user = PerRep.Table.FirstOrDefault(w => w.AppOperationId == appPermission.Id);

            if (user != null)
            {
                return DomainResult<AppPermission>.Error("权限已存在");
            }
            var permission = await PerRep.CreateAsync(appPermission);

            return DomainResult<AppPermission>.Success(permission);
        }

        public async Task<DomainResult<AppPermission>> DeletePermission(Guid PerId)
        {
            var permission = await PerRep.DeleteAsync(PerId);
            if (permission == null)
            {
                return DomainResult<AppPermission>.Error("权限不存在");
            }
            return DomainResult<AppPermission>.Success(permission);
        }

        public async Task<DomainResult<List<AppPermission>>> GetPermissionWithPagination(int page, int pageSize)
        {
            var PerList = await PerRep.GetListAsync();

            if (PerList.Count == 0)
            {
                return DomainResult<List<AppPermission>>.Error("当前还没有任何用户");
            }
            var PerListPage = PerList.Skip((page - 1) * pageSize).Take(pageSize).Where(item => item.IsDeleted == false).ToList();

            return DomainResult<List<AppPermission>>.Success(PerListPage);
        }

        public async Task<DomainResult<AppPermission>> GetAppPermissionById(Guid id)
        {
            var permission = await PerRep.GetByIdAsync(id);
            if (permission == null)
            {
                return DomainResult<AppPermission>.Error("权限不存在");
            }
            return DomainResult<AppPermission>.Success(permission);
        }

        public Task<DomainResult<AppPermission>> UpdatePermissionProfile(Guid Id, AppPermission entity)
        {
            throw new NotImplementedException();
        }
    }
}
